package com.youkeda.excise;

/**
 * @author CoderXshuai
 * @date 2020/7/17/0017 0:28
 */

public class Find {
    public static int BinaryFind(int[] array, int aim) {
        int middle = array.length / 2;
        int middleValue = array[middle];
        int left = 0;
        int right = array.length;
        while (left <= right) {
            if (aim < middleValue) {
                right = middle - 1;
            } else if (aim > middleValue) {
                left = middle + 1;
            } else {
                return middle;
            }
            //重新赋值
            middle = (left + right) / 2;
            middleValue = array[middle];
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] array = {2, 3, 22, 75, 80, 90, 100};
        int result1 = BinaryFind(array, 2);
        if (result1 == -1) {
            System.out.println("22 不存在数组中");
        } else {
            System.out.println("22 存在数组中，索引值是 " + result1);
        }

        int result2 = BinaryFind(array, 50);
        if (result2 == -1) {
            System.out.println("50 不存在数组中");
        } else {
            System.out.println("50 存在数组中，索引值是 " + result2);
        }
    }
}
